<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>对象</title>
  </head>
  <body>
    <script>
      // 1、利用字面量创建(常用)
      var obj = {
        name: '张三',
        age: 20,
        hobby: function (arg) {
          console.log('我的爱好是：', arg);
        }
      };
      console.log('姓名--', obj.name);
      console.log('年龄--', obj['age']);
      obj.hobby('打游戏');

      // 2、利用 new 创建
      var obj2 = new Object();
      obj2.name = '李四';
      obj2.age = 30;
      obj2.hobby = function (arg) {
        console.log('我的爱好是：', arg);
      };
      console.log('姓名：' + obj2.name + '\n年龄：' + obj2['age']);
      obj2.hobby('赛车');

      // 3、构造函数创建
      function Star(name, sex, age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.sing = function (song) {
          console.log('唱歌--', song);
        };
      }
      var ldh = new Star('刘德华', '男', 18);
      console.log('刘德华个人信息：' + '\n姓名：' + ldh.name + '\n年龄：' + ldh['age']);
      ldh.sing('冰雨');

      var zxy = new Star('张学友', '男', 20);
      console.log('张学友个人信息：' + '\n姓名：' + zxy.name + '\n年龄：' + zxy['age']);
      zxy.sing('李香兰');

      // 4、遍历对象
      var obj3 = {
        name: '王五',
        sex: '女',
        age: 10,
        hobby: function (arg) {
          console.log('我的爱好是：', arg);
        }
      };
      for (var k in obj3) {
        console.log(k + '----' + obj3[k]); // k:属性名
      }
    </script>
  </body>
</html>
